﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Services;

using com.vs.DataAccess;
using com.vs.DataAccess.DO;
using com.vs.General;
using com.vs.Task.BL;

namespace com.vs.WebService
{
    /// <summary>
    /// Summary description for TaskService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class TaskService : System.Web.Services.WebService
    {
        //string XSLT_DIR = "xslt";

        [WebMethod]
        public string GetTaskHdrs(string stSessID, string stCondXML)
        {
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;
            TaskManager mgr = null;
            string stErr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;
                dcCond = new List<KeyValuePair<string, KeyValuePair<string, object>>>();
                if (!StringManager.IsEmpty(stCondXML))
                    dcCond = XMLDocManager.BuildQueryCondition(stCondXML);


                //dt = TaskHeaderDO.GetTableByColumns(sess, dcCond, null);
                mgr = new TaskManager();
                dt = mgr.GetTaskHdrsByQFCri(sess, dcCond, null, ref stErr);
                if (!StringManager.IsEmpty(stErr))
                    return stErr;

                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "TASK_HEADER";
                dt.DataSet.DataSetName = "TASK_HEADERS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetTaskDtls(string stSessID, string stIntTaskNum)
        {
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;
            int iTaskNum = 0;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                if (StringManager.IsEmpty(stIntTaskNum))
                    return null;
                iTaskNum = Int32.Parse(stIntTaskNum);

                dt = TaskDetailDO.GetTableByColumn(sess, "INTERNAL_TASK_NUM", iTaskNum);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "TASK_DETAIL";
                dt.DataSet.DataSetName = "TASK_DETAILS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string ConfirmTaskDtls(string stSessID, string[] arConfirmDSXML)
        {
            Session sess = null;
            StringBuilder sbError = new StringBuilder();
            string stErr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                foreach (string stConfirmDSXML in arConfirmDSXML)
                {
                    stErr = this.ConfirmTaskDtl(sess, stConfirmDSXML);
                    if (!StringManager.IsEmpty(stErr))
                        sbError.AppendLine(stErr);
                }

                if (StringManager.IsEmpty(sbError))
                    return null;
                else
                    return sbError.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        public string ConfirmTaskDtl(Session sess, string stConfirmDSXML)
        {
            TaskConfirmDS tcDS = null;
            StringBuilder sbXML = new StringBuilder();
            TaskExecutionHelper tsMgr = null;

            try
            {
                tcDS = (TaskConfirmDS)XMLDocManager.Deserialize(typeof(TaskConfirmDS), XMLDocManager._XML_HEADER + stConfirmDSXML);
                if (tcDS == null)
                    throw new Exception("反序列化错误");

                tsMgr = new TaskExecutionHelper();
                return tsMgr.ConfirmTask(sess, tcDS);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string ConfirmTask(string stSessID, int iTaskNum)
        {
            Session sess = null;
            TaskExecutionHelper tsMgr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                tsMgr = new TaskExecutionHelper();
                return tsMgr.ConfirmTask(sess, iTaskNum);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string SplitTask(String stSessID, int iTaskNum, int iQty)
        {
            Session sess = null;
            TaskExecutionHelper tsMgr = null;
            string stErr = null;
            int iNewTaskNum = 0;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                tsMgr = new TaskExecutionHelper();
                iNewTaskNum = tsMgr.SplitTaskDetail(sess, iTaskNum, iQty, ref stErr);
                if (iNewTaskNum <= 0)
                {
                    if (!StringManager.IsEmpty(stErr))
                        return stErr;
                    else
                        return MessageManager.MSG_TSK_011;//"拆分任务明细失败"
                }
                return null;
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string OverridePick(String stSessID, int iTaskNum, string stOverrideLoc, string stOverrideLPN)
        {
            Session sess = null;
            TaskExecutionHelper tsMgr = null;
            string stErr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                tsMgr = new TaskExecutionHelper();
                return tsMgr.OverridePickTask(sess, iTaskNum, stOverrideLoc, stOverrideLPN);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }

        }

        [WebMethod]
        public string OverridePutaway(String stSessID, int iTaskNum, string stOverrideLoc, string stOverrideLPN)
        {
            Session sess = null;
            TaskExecutionHelper tsMgr = null;
            string stErr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                tsMgr = new TaskExecutionHelper();
                return tsMgr.OverridePutawayTask(sess, iTaskNum, stOverrideLoc, stOverrideLPN);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }

        }
    }
}
